/*
 * Copyright (C) 2006-2012 TongYan Corporation
 * All rights reserved.
 *
 * @brief info:
 *
 * @author: 王哲成 - wangzhecheng@yeah.net
 * @date: 2021.05.19
 * @last modified: 2021-05-19 18:00
 *
 */

#ifndef __LOCAL_SEARCH_METHOD_HEADER__
#define __LOCAL_SEARCH_METHOD_HEADER__

#include "ISGBackAnalysisMethod.hpp"
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iterator>

inline int random(const int &x) { return rand() % (x); }
inline double random_between(const double &lower, const double &upper) {
  return lower +
         std::abs(upper - lower) * random(100) / static_cast<double>(100);
}

class LocalSearchMethod : public ISGBackAnalysisMethod {
public:
  LocalSearchMethod() = delete;
  LocalSearchMethod(const ISGObjectFunction *const ptr, const size_t &nseed,
                    const double &epsilon)
      : ISGBackAnalysisMethod{ptr}, m_nSeedNum{nseed}, m_dEpsilon{epsilon} {}
  virtual ~LocalSearchMethod() = default;

  void setSeedNumber(const size_t &n) { m_nSeedNum = n; }
  size_t getSeedNumber() const { return m_nSeedNum; }

  virtual Candidate getOptimalSolution(const ParamSet &param_set) override;

protected:
  size_t m_nSeedNum;
  double m_dEpsilon;

protected:
  virtual bool updateSearchPoint(const ParamSet &, Candidate &) const = 0;
  /* virtual bool convergence(const ParamSet &) const = 0; */

  double Armijo(
      const Vector &x,                   // x的值
      const Vector &dx,                  // x偏移量（求差分用）
      const Vector &dk,                  // f(x)下降方向
      const double &gamma = 0.4, // 参数gamma,斜率折减系数，取值(0,0.5)越大越快
      const double &sigma = 0.5, // 参数sigma,步长缩减比例，取值(0,1),越大越慢
      const size_t &iterMax = 100) const; //最大迭代次数
};

#endif
